package cn.stylefeng.guns.modular.yingbao.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.SysUser;
import cn.stylefeng.guns.modular.yingbao.entity.UserMoney;
import cn.stylefeng.guns.modular.yingbao.entity.UserMoneyLog;
import cn.stylefeng.guns.modular.yingbao.entity.UserMoneySetting;
import cn.stylefeng.guns.modular.yingbao.mapper.UserMoneyMapper;
import cn.stylefeng.guns.modular.yingbao.model.params.UserMoneyParam;
import cn.stylefeng.guns.modular.yingbao.model.result.UserMoneyResult;
import cn.stylefeng.guns.modular.yingbao.service.UserMoneyLogService;
import  cn.stylefeng.guns.modular.yingbao.service.UserMoneyService;
import cn.stylefeng.guns.modular.yingbao.service.UserMoneySettingService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户金额 服务实现类
 * </p>
 *
 * @author zs
 * @since 2019-12-10
 */
@Service
public class UserMoneyServiceImpl extends ServiceImpl<UserMoneyMapper, UserMoney> implements UserMoneyService {

    @Autowired
    UserMoneySettingService userMoneySettingService;
    @Autowired
    UserMoneyLogService userMoneyLogService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(UserMoneyParam param){
        UserMoney entity = getEntity(param);
        this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(UserMoneyParam param){
        this.removeById(getKey(param));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(UserMoneyParam param){
        UserMoney oldEntity = getOldEntity(param);
        UserMoney newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public UserMoneyResult findBySpec(UserMoneyParam param){
        return null;
    }

    @Override
    public List<UserMoneyResult> findListBySpec(UserMoneyParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(UserMoneyParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData appOut(Long userid) {
        if (userid == null){
            return ResponseData.error("参数错误");
        }

        UserMoney userMoney = baseMapper.selectOne(new QueryWrapper<UserMoney>().eq("userid", userid));
        if (userMoney==null){
            return ResponseData.error("没有资金表");
        }
        if (userMoney.getBackup1()>0){
            return ResponseData.error("资金还在冻结");
        }
        userMoney.setAvailMoney(userMoney.getAvailMoney()+userMoney.getFrozenCapital());
        userMoney.setFrozenCapital(0L);
        long version = userMoney.getVersion();
        userMoney.setVersion(version+1);
        boolean update = this.update(userMoney, new QueryWrapper<UserMoney>().eq("id", userMoney.getId()).eq("version", version));
        return update ? ResponseData.success(): ResponseData.error("错误！");
    }

    @Override
    @Transactional(rollbackFor = Exception.class,timeout = 360)
    public void updateDay() {
        List<UserMoney> userMoneyList = baseMapper.selectList(new QueryWrapper<UserMoney>().gt("frozen_capital",0));
        if (userMoneyList.size()<1){
            return;
        }
        for (UserMoney userMoney:userMoneyList){
            userMoney.setBackup1(userMoney.getBackup1()-1);
            UserMoneySetting userMoneySetting = userMoneySettingService.getOne(null);
            userMoney.setAvailMoney(userMoney.getAvailMoney()+userMoney.getFrozenCapital()*userMoneySetting.getFrozenRebate()/100);
            UserMoneyLog userMoneyLog=new UserMoneyLog();
            userMoneyLog.setUserid(userMoney.getUserid());
            userMoneyLog.setRemark("冻结返利");
            userMoneyLog.setCreateTime(new Date());
            userMoneyLog.setMoney(userMoney.getFrozenCapital()*userMoneySetting.getFrozenRebate()/100);
            userMoneyLog.setPayType(5);
            userMoneyLog.setStuats(1);
            userMoneyLogService.save(userMoneyLog);
            long version = userMoney.getVersion();
            userMoney.setVersion(version+1);
            int update = baseMapper.update(userMoney, new QueryWrapper<UserMoney>().eq("id", userMoney.getId()).eq("version", version));
            if (update>0){
                System.out.println("成功!");
            }
        }
    }

    private Serializable getKey(UserMoneyParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private UserMoney getOldEntity(UserMoneyParam param) {
        return this.getById(getKey(param));
    }

    private UserMoney getEntity(UserMoneyParam param) {
        UserMoney entity = new UserMoney();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
